/*
 ******************************************************************************
 *
 *       Copyright ASTRI, 2016.  All rights reserved.
 *
 *   This software is proprietary to and embodies the confidential technology
 *   of Hong Kong Applied Science and Technology Research Institute Company
 *   Limited (ASTRI).
 *   Possession, use, or copying of this software and media is authorized
 *   only pursuant to a valid written license from ASTRI or an authorized
 *   sublicensor.
 *
 * File Name:
 *   XXX.java
 *
 * Purpose:
 *   
 *   
 *   
 *
 * Project or Modules Name:
 *   
 *
 * Modules Contained:
 *   N/A
 *
 * Modification History:
 * Date(DDMMYY)  Version No Modified By  Description
 * ------------  ---------- -----------  -----------
 * 20160405       0.0.0.1    XXX     Created
 *
 ******************************************************************************
 */


package com.j.thetabk.thetas;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by user on 14/4/2016.
 */
public class ThetaSProperty {

    public static String [] propertyName={
            "whiteBalance",//multiple
            "iso",
            "shutterSpeed",
            "exposureCompensation",
            "_filter",//single

            "exposureProgram",
            "captureMode",
            "offDelay",
            "sleepDelay",
            "_HDMIreso",

            "fileFormat",
            "aperture",
            "exposureDelay"
    };

    public static JSONArray ExposureDelaySupport()
    {
        JSONArray ja=new JSONArray();
        for(int i=0;i<11;i++)
        ja.put(i);
                return ja;
    }

    public JSONArray support=null;
    public int choice;
    public String [] supportDisplay=null;

    public ThetaSProperty()
    {
        choice=0;
        support=null;
        supportDisplay=null;
    }

    public static String EVString(double v)
    {
        return String.format("%+.1f", v);
    }

    public static String ShutterString(double v)
    {
        if(v<1)
            return "1/"+ShutterString(1/v);
        if(v<(int)v+0.1)
            return String.valueOf((int) v);
        return String.format("%.1f", v);
    }

    public static String FormatString(JSONObject jb) throws JSONException {
        return jb.getString("width")+"x"+jb.getString("height")+" "+jb.getString("type");
    }

    public static String DelayString(int delays)
    {
        if(delays==65535)
            return "Never";
        return delays/60+"min";
    }

    protected int ParseChoiceA(String propertyStr) throws JSONException {
        if(support==null || support.length()<1)
            return -1;
        for(int i=0;i<support.length();i++) {
            String str=support.getString(i);
            if (str.equals(propertyStr))
                return i;
        }
        return -2;
    }

    public void ParseChoice(int index, JSONObject jbProperties) throws JSONException {
        String propertyStr = jbProperties.getString(propertyName[index]);
        choice=ParseChoiceA(propertyStr);
    }

    public void ParseChoiceAndSupport(int index, JSONObject jbProperties) throws JSONException {
        support =jbProperties.getJSONArray(propertyName[index]+ThetaSProtocol.support);
        ParseChoice(index,jbProperties);
        UpdateDisplayList(index);
    }

    protected void UpdateDisplayList(int index) throws JSONException {

        switch(index) {
            case 0:
                supportDisplay=ThetaSProtocol.StringListFromArray(support);
                return;
            case 1:
                supportDisplay=ThetaSProtocol.StringListFromArray(support);
                return;
            case 2:
                supportDisplay= new String[support.length()];
                for(int i=0;i<support.length();i++)
                {
                    supportDisplay[i]=ShutterString(support.getDouble(i));
                }
                return;
            case 3:
                supportDisplay= new String[support.length()];
                for(int i=0;i<support.length();i++)
                {
                    supportDisplay[i]=EVString(support.getDouble(i));
                }
                return;
            case 4:
                supportDisplay=ThetaSProtocol.StringListFromArray(support);
                return;
            case 5:
                supportDisplay= new String[]{"Manual", "Auto", "Shutter priority", "ISO priority"};
                return;
            case 6:
                supportDisplay= new String[]{"Image", "Video"};
                return;
            case 7:
                supportDisplay= new String[support.length()];
                for(int i=0;i<support.length();i++)
                {
                    supportDisplay[i]=DelayString(support.getInt(i));
                }
                return;
            case 8:
                supportDisplay= new String[support.length()];
                for(int i=0;i<support.length();i++)
                {
                    supportDisplay[i]=DelayString(support.getInt(i));
                }
                return;
            case 9:
                supportDisplay=ThetaSProtocol.StringListFromArray(support);
                return;
            case 10:
                supportDisplay= new String[support.length()];
                for(int i=0;i<support.length();i++)
                {
                    supportDisplay[i]=FormatString(support.getJSONObject(i));
                }
                return;
            case 11:
                supportDisplay=ThetaSProtocol.StringListFromArray(support);
                return;
            case 12:
                supportDisplay=ThetaSProtocol.StringListFromArray(support);
                return;
            default:
                supportDisplay=ThetaSProtocol.StringListFromArray(support);
                return;
        }
    }

    public String DisplayChoice(int index)
    {
        if(choice<0)
            return null;

        switch(index)
        {
            case 0:
                return supportDisplay[choice];
            case 1:
                return "ISO "+supportDisplay[choice];
            case 2:
                return supportDisplay[choice];
            case 3:
                return "EV"+supportDisplay[choice];
            case 4:
            {
                String [] eplist={"OFF","DR","NR","HDR"};
                return eplist[choice];
            }
            case 5:
            {
                String [] eplist={"M","Auto","S","P"};
                return eplist[choice];
            }
            case 6:
                return supportDisplay[choice];
            case 7:
                return supportDisplay[choice];
            case 8:
                return supportDisplay[choice];
            case 9:
                return supportDisplay[choice];
            case 10:
                return supportDisplay[choice];
            case 11:
                return "F"+supportDisplay[choice];
            case 12:
                return "-"+supportDisplay[choice]+"s";
            default:
                return null;
        }

    }

    public JSONObject PropertyItem(int index) throws JSONException {
        JSONObject jbtemp2=new JSONObject();
        jbtemp2.put(propertyName[index], support.get(choice));
        return jbtemp2;
    }


}
